import java.util.*;

public class Solution1233 {

    static class Trie{

        HashMap<String,Trie> map=new HashMap<>();
        boolean end=false;

        public void insert(String[] strings){
            Trie trie=this;
            for (String string : strings) {
                if (!trie.map.containsKey(string)) {
                    trie.map.put(string, new Trie());
                }
                trie=trie.map.get(string);
            }
            trie.end=true;
        }

        public boolean search(String[] strings){
            Trie trie=this;
            for (String string : strings) {
                if (!trie.map.containsKey(string)){
                    return false;
                }
                trie=trie.map.get(string);
                if (trie.end){
                    return true;
                }
            }
            return false;
        }

    }

    public List<String> removeSubfolders(String[] folder) {
        Arrays.sort(folder, Comparator.comparingInt(String::length));
        List<String> list=new ArrayList<>();
        Trie trie=new Trie();
        for (String s : folder) {
            String[] strings=s.substring(1).split("/");
            if (!trie.search(strings)){
                list.add(s);
                trie.insert(strings);
            }
        }
        return list;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1233().removeSubfolders(new String[]{"/a","/a/b","/c/d","/c/d/e","/c/f"}));
    }
}
